﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace TSTreeThesis.DataMgr
{
   public class FileData : Data
    {
        private String fileName;

        public FileData(string fileName, int compressionRate)
            : base(compressionRate)
        {
            this.fileName = fileName;
        }

        public FileData(int compressionRate)
            : base(compressionRate)
        {
            fileName = null;
        }

        public void SetFileName(string fileName)
        {
            this.fileName = fileName;
        }

        public string GetFileName()
        {
            return fileName;
        }

        public override void readData(int fromIndex, int toIndex)
        {
            if (fileName == null)
                throw new Exception("fileName is null!");

            int curLine = 0;
            ArrayList tempData = new ArrayList();

            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    StreamReader reader = new StreamReader(fs);
                    String line;
                    int count = 0;
                    fs.Position = 0;
                    while ((line = reader.ReadLine()) != null && curLine < toIndex)
                    {
                        //* .Double. .Double.double value;*/
                        if (curLine >= fromIndex && curLine < toIndex)
                        {
                            tempData.Add(Convert.ToDouble(line));
                            count++;
                        }
                        curLine++;
                    }
                    reader.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            dataLength = tempData.Count;
            originalData = new double[dataLength];
            tempData.CopyTo(originalData);
        }
    }
}
